import time

import allure
from selenium.common import StaleElementReferenceException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from page.base_page import BasePage


class ApproveSelfOrder(BasePage):
    __self_order_list = By.XPATH, "//*[@href='/order/selfOrder/orderApply']"
    __APPROVE_BUTTON = By.XPATH, "//*[text()='通过']"
    __APPROVE_REASON = By.XPATH, "//*[@placeholder='请输入审批意见']"
    __APPROVE_OK_BUTTON = By.XPATH, "//*[@class='el-button el-button--primary el-button--default']//*[text()='确定']"
    __APPROVE_CANCEL_BUTTON = By.XPATH, "//*[@class='el-button el-button--default el-button--small']//*[text()='取消']"
    __APPROVE_REASON_ERROR_MESSAGE = By.XPATH, "//*[@class='el-form-item is-error']//*[@class='el-form-item__error']"
    __APPROVE_REASON_ERROR_MESSAGE_TEXT = "审批意见不能为空"
    __APPROVE_SUCCESS_MESSAGE = By.XPATH, "//*[@class='el-message__content']"
    __APPROVE_SUCCESS_MESSAGE_TEXT = "审批成功"
    __APPROVE_FAIL_MESSAGE = By.XPATH, "//*[@class='el-message__content']"
    __APPROVE_FAIL_MESSAGE_TEXT = "审批失败"
    __APPROVE_CANCEL_MESSAGE = By.XPATH, "//*[@class='el-message__content']"
    __APPROVE_CANCEL_MESSAGE_TEXT = "已取消审批"
    __NAME = By.XPATH, "//*[@placeholder = '请输入借贷人姓名']"
    __PHONE = By.XPATH, "//*[@placeholder = '请输入借贷人手机号']"
    __SSN = By.XPATH, "//*[@placeholder = '请输入借贷人身份证号']"
    __NUMBER = By.XPATH, "//*[@placeholder = '请输入借贷编号']"
    __CAPITAL = By.XPATH, "//*[text()='请选择资金方']"
    __PRODUCT_NAME = By.XPATH, "//*[@placeholder = '请输入产品名称']"
    __ORDER_STATUS = By.XPATH, "//*[text()='请选择订单状态']"
    __CAPITAL_PLAN = By.XPATH, "//*[text()='请选择配资方案']"
    __CREATION_START_TIME = By.XPATH, "//*[@placeholder = '开始日期']"
    __CREATION_END_TIME = By.XPATH, "//*[@placeholder = '结束日期']"
    __SEARCH_BUTTON = By.XPATH, "//*[@class = 'el-button el-button--primary el-button--default']"
    __SEARCH_FIRST_DETAILS = By.XPATH, "//*[@class = 'el-table__row'][1]/td[2]/div/a/span"
    __SEARCH_FIRST_PRODUCT_TYPE = By.XPATH, "//*[@class = 'el-table__row'][1]/td[3]/div"
    __SEARCH_FIRST_NAME = By.XPATH, "//*[@class = 'el-table__row'][1]/td[4]/div"
    __SEARCH_FIRST_ORDER_STATUS = By.XPATH, "//*[@class = 'el-table__row'][1]/td[6]/div/span/span"
    __SEARCH_FIRST_CAPITAL = By.XPATH, "//*[@class = 'el-table__row'][1]/td[7]/div"
    __SEARCH_FIRST_PRODUCT_NAME = By.XPATH, "//*[@class = 'el-table__row'][1]/td[8]/div"
    __VIEW_REMARKS = By.XPATH, "//*[@class = 'el-table__row'][1]/td[16]/div/a"
    __SEARCH_FIRST_CREATION_TIME = By.XPATH, "//*[@class = 'el-table__row'][1]/td[17]/div"
    __SEARCH_QUANTITY = By.XPATH, "//*[@class = 'el-pagination__total is-first']"
    __DETAILS_PHONE = By.XPATH, "//*[text()='手机号']/following-sibling::span[1]"
    __DETAILS_SSN = By.XPATH, "//*[text()='身份证']/following-sibling::span[1]"
    __MAIN_PAGE_BUTTON = By.XPATH, "//*[@role = 'menuitem']/*[text()='首页']"
    __DETAILS_CLOSE_BUTTON = By.XPATH, "//*[@class = 'el-icon el-drawer__close']"
    __RESET_BUTTON = By.XPATH, "//*[text() ='重置']"
    __EXPORT_BUTTON = By.XPATH, "//*[text()='导出 ']/.."
    __CUE_WORD = By.XPATH, "//*[@class = 'el-message el-message--error']/p"
    __FIRST_ORDER_APPROVE_BUTTON = By.XPATH, "//*[@class = 'el-table__row'][1]/td[18]//*[text()='审批']"
    __APPROVE_REMARK = By.XPATH, ("//*[@class = 'scroll-content-detail']//*[@class = 'el-textarea el-input--default']"
                                  "/textarea")
    __APPROVE_SUBMIT = By.XPATH, "//*[@class = 'scroll-content-detail']//*[text()='提交']"
    __SECOND_OK = By.XPATH, "//*[@aria-label ='再次确认']//*[text()='确定']"
    __FINAL_AMOUNT = By.XPATH, "//*[text()='终审金额（元）：']/..//input"
    __FINAL_RATE = By.XPATH, "//*[text()='终审利率（月）：']/..//input"
    __FINAL_PERIOD = By.XPATH, "//*[text()='请选择还款方式']"
    __FINAL_REPAYMENT_WAY = By.XPATH, ("//*[text()='还款方式：']/.."
                                       "//*[@class = 'el-select__selected-item el-select__placeholder']")
    __FINAL_GUARANTEE_FEE = By.XPATH, "//*[text()='请选择担保费类型']"
    __FINAL_GUARANTEE_FEE_AMOUNT = By.XPATH, "//*[text()='担保费：']/..//*[@class = 'el-input__inner']"
    __FINAL_SERVICE_FEE = By.XPATH, "//*[text()='服务费：']/..//*[@class = 'el-input__inner']"
    __FINAL_REMARK = By.XPATH, "//*[text()='终审意见：']/..//*[@class = 'el-textarea__inner']"

    def goto_main_page(self):
        from page.main_page import MainPage
        self.find_and_click(self.__MAIN_PAGE_BUTTON)
        return MainPage(self.driver)

    def goto_self_order(self):
        with allure.step('点击订单列表'):
            from page.order_apply import OrderApply
            self.find_and_click(self.__self_order_list)
            self.wait(EC.staleness_of(self.find(By.XPATH, "//*[text()='借贷人姓名']")))
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return OrderApply(self.driver)

    def search_name(self, name):
        with allure.step(f"输入{name}的姓名进行查询"):
            self.find_and_send(self.__NAME, text=name)
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def search_phone(self, phone):
        with allure.step(f"输入{phone}手机号码进行查询"):
            self.find_and_send(self.__PHONE, text=phone)
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def search_ssn(self, ssn):
        with allure.step(f"输入{ssn}身份证号码进行查询"):
            self.find_and_send(self.__SSN, text=ssn)
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def search_number(self, number):
        with allure.step(f"输入{number}订单编号进行查询"):
            self.find_and_send(self.__NUMBER, text=number)
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def search_capital(self, capital):
        with allure.step(f"选择资金方{capital}"):
            self.find_and_click(self.__CAPITAL)
            self.find_and_click(By.XPATH, f"//*[@class ='el-scrollbar']//li/*[text() = '{capital}']")
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def search_product_name(self, product_name):
        with allure.step(f"输入{product_name}产品名称进行查询"):
            self.find_and_send(self.__PRODUCT_NAME, text=product_name)
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def search_order_status(self, order_status):
        with allure.step(f"选择订单状态{order_status}"):
            self.find_and_click(self.__ORDER_STATUS)
            self.find_and_click(By.XPATH, f"//*[@class ='el-scrollbar']//li/*[text() = '{order_status}']")
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def search_capital_plan(self, capital_plan):
        with allure.step(f"选择配资方案{capital_plan}"):
            self.find_and_click(self.__CAPITAL_PLAN)
            self.find_and_click(By.XPATH, f"//*[@class ='el-scrollbar']//li/*[text() = '{capital_plan}']")
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def search_creation_time(self, creation_start_time, creation_end_time):
        with allure.step(f"选择创建时间{creation_start_time}至{creation_end_time}"):
            self.find_and_send(self.__CREATION_START_TIME, text=creation_start_time)
            self.find_and_send(self.__CREATION_END_TIME, text=creation_end_time)
            self.find_and_send(self.__CREATION_END_TIME, text=Keys.ENTER)
            self.find_and_click(self.__SEARCH_BUTTON)
        return ApproveSelfOrder(self.driver)

    def goto_search_results_details(self):
        with allure.step("点击当前查询结果第一位的订单详情"):
            self.find_and_click(self.__SEARCH_FIRST_DETAILS)
        return ApproveSelfOrder(self.driver)

    def close_details(self):
        with allure.step("点击详情页关闭按钮"):
            ele = self.finds(self.__DETAILS_CLOSE_BUTTON)[0]
            ele.click()
        return ApproveSelfOrder(self.driver)

    def get_search_results_name(self):
        with allure.step("获取当前查询结果第一位的姓名/公司名"):
            time.sleep(0.2)
            search_first_name = self.find(self.__SEARCH_FIRST_NAME).text
        return search_first_name

    def get_search_results_phone(self):
        with allure.step("获取当前订单手机号码"):
            search_first_phone = self.find(self.__DETAILS_PHONE).text
        return search_first_phone

    def get_search_results_ssn(self):
        with allure.step("获取当前订单身份证号码"):
            search_first_phone = self.find(self.__DETAILS_SSN).text
        return search_first_phone

    def get_search_results_number(self):
        with allure.step("获取当前订单编号"):
            time.sleep(0.2)
            search_first_number = self.find(self.__SEARCH_FIRST_DETAILS).text
        return search_first_number

    def get_search_results_capital(self):
        with allure.step("获取当前资金方公司"):
            time.sleep(0.2)
            search_first_capital = self.find(self.__SEARCH_FIRST_CAPITAL).text
        return search_first_capital

    def get_search_results_product_name(self):
        with allure.step("获取当前产品名称"):
            time.sleep(0.2)
            search_first_product_name = self.find(self.__SEARCH_FIRST_PRODUCT_NAME).text
        return search_first_product_name

    def get_search_results_order_status(self):
        with allure.step("获取当前订单状态"):
            time.sleep(0.2)
            search_first_order_status = self.find(self.__SEARCH_FIRST_ORDER_STATUS).text
        return search_first_order_status

    def get_search_results_product_type(self):
        with allure.step("获取当前借款人类型"):
            search_first_product_type = self.find(self.__SEARCH_FIRST_PRODUCT_TYPE).text
        return search_first_product_type

    def get_search_results_creation_time(self):
        with allure.step("获取当前创建时间"):
            search_first_creation_time = self.find(self.__SEARCH_FIRST_CREATION_TIME).text
        return search_first_creation_time

    def get_search_quantity(self):
        with allure.step("获取当前查询结果数量"):
            search_quantity = self.find(self.__SEARCH_QUANTITY).text
        return search_quantity

    def click_reset_button(self):
        with allure.step("点击重置按钮"):
            self.find_and_click(self.__RESET_BUTTON)
        return ApproveSelfOrder(self.driver)

    def export_button(self):
        with allure.step("点击导出按钮"):
            self.find_and_click(self.__EXPORT_BUTTON)
        return ApproveSelfOrder(self.driver)

    def get_cue_word(self):
        with allure.step("获取提示词"):
            time.sleep(1)
            cue_word = self.find(self.__CUE_WORD).text
        return cue_word

    def click_first_order_approve_button(self):
        with allure.step("点击第一条订单的审批按钮"):
            while True:
                try:
                    self.find_and_click(self.__FIRST_ORDER_APPROVE_BUTTON)
                    break
                except StaleElementReferenceException:
                    pass
        return ApproveSelfOrder(self.driver)

    def select_order_approve_status(self, approve_status, approve_remark=None):
        with allure.step(f"选择审批状态{approve_status}"):
            self.find_and_click(By.XPATH, f"//*[@class = 'el-radio-group']//*[text() = '{approve_status}']")
        if approve_remark is not None:
            with allure.step(f"输入审批备注{approve_remark}"):
                self.find_and_send(self.__APPROVE_REMARK, text=approve_remark)
        return ApproveSelfOrder(self.driver)

    def click_approve_submit_button(self):
        with allure.step("点击审批提交按钮"):
            self.find_and_click(self.__APPROVE_SUBMIT)
        return ApproveSelfOrder(self.driver)

    def click_approve_submit_second_button(self):
        self.find_and_click(self.__SECOND_OK)
        return ApproveSelfOrder(self.driver)

    def final_approve(self, final_amount=None, final_period=None, final_rate=None, final_repayment_way=None,
                      final_guarantee_fee=None, final_guarantee_fee_amount=None, final_service_fee=None,
                      final_remark=None, final_result=None):
        if final_amount is not None:
            with allure.step(f"输入终审金额为{final_amount}"):
                self.find_and_send(self.__FINAL_AMOUNT, text=final_amount)
        if final_rate is not None:
            with allure.step(f"输入终审利率为{final_rate}"):
                self.find_and_send(self.__FINAL_RATE, text=final_rate)
        if final_period is not None:
            with allure.step(f"选择终审期数为{final_period}"):
                self.find_and_click(self.__FINAL_PERIOD)
                self.find_and_click(By.XPATH, f"//*[@class ='el-scrollbar']//li/*[text() = '{final_period}']")
        if final_repayment_way is not None:
            with allure.step(f"选择还款方式为{final_repayment_way}"):
                self.find_and_click(self.__FINAL_REPAYMENT_WAY)
                self.find_and_click(By.XPATH, f"//*[@class ='el-scrollbar']//li/*[text() = '{final_repayment_way}']")
        if final_guarantee_fee is not None:
            with allure.step("选择有无担保费"):
                self.find_and_click(self.__FINAL_GUARANTEE_FEE)
                self.find_and_click(By.XPATH, f"//*[@class ='el-scrollbar']//li/*[text() = '{final_guarantee_fee}']")
        if final_guarantee_fee_amount is not None:
            with allure.step(f"输入担保费为{final_guarantee_fee_amount}"):
                self.find_and_send(self.__FINAL_GUARANTEE_FEE_AMOUNT, text=final_guarantee_fee_amount)
        if final_service_fee is not None:
            with allure.step(f"输入服务费为{final_service_fee}"):
                self.find_and_send(self.__FINAL_SERVICE_FEE, text=final_service_fee)
        if final_remark is not None:
            with allure.step(f"输入终审意见为{final_remark}"):
                self.find_and_send(self.__FINAL_REMARK, text=final_remark)
        if final_result is not None:
            with allure.step(f"选择终审结果为{final_result}"):
                self.find_and_click(By.XPATH, f"//*[@class = 'el-radio-group']//*[text() = '{final_result}']")
        return ApproveSelfOrder(self.driver)

    def get_input_value(self, model):
        with allure.step(f"获取{model}输入框已输入的值"):
            input_value = self.find(By.XPATH, f"//*[text() = '{model}']/../div/div/input").get_attribute("value")
        return input_value

